#include "delegate.h"

int Delegate::itemWidth = 0;
QFont Delegate::mFont;

Delegate::Delegate(QObject *parent) : QStyledItemDelegate(parent)
{
    itemWidth = 1024 - 75 -10;
    mFont.setPixelSize(30);
}

void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid())
    {

        QString name = index.data(MyModel::Role_Name).toString();
        bool isDir = index.data(MyModel::Role_Type).toInt();
        bool select = index.data(MyModel::Role_Select).toBool();
        int x = index.data(MyModel::Role_X).toInt();

        {
            painter->save();
            QRectF rect(option.rect);
            rect.setX(10);
            rect.setY(rect.y() + 15);
            rect.setWidth(60);
            rect.setHeight(60);

            QImage img;
            if(isDir)
                img.load(":/res/fold.png");
            else
                img.load(":/res/mp4.png");
            img.scaled(60, 60);
            painter->drawImage(rect, img);

            painter->restore();
        }

        painter->save();


        //int fontSize = index.data(MyModel::Role_FontSize).toInt();

        QRectF rect(option.rect);
        painter->setFont(mFont);
        if(select)
        {
            rect.setX(x +75+10);
            painter->setPen(QPen("#2094fa"));
            painter->drawText(rect, Qt::AlignVCenter, name);
        }
        else
        {
            rect.setX(75+10);
            painter->setPen(QPen(Qt::white));
            painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, name);
        }

        painter->restore();
    }
}

QSize Delegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
{
    return QSize(1024, 90);
}

